<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="utf-8">
    <title>字典列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/static/layuiadmin/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/static/layuiadmin/style/admin.css}" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-col-md12">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">字典名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="dictName" placeholder="请输入字典名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">字典别名</label>
                        <div class="layui-input-block">
                            <input type="text" name="dictKey" placeholder="请输入字典别名" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="dictKeySearch">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button sec:authorize="hasAuthority('dict:add')" class="layui-btn layuiadmin-btn-useradmin" data-type="addView">添加</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md7">
            <div class="layui-card-body">
                <table id="dictTypeTable" lay-filter="dictTypeTable"></table>
                <script type="text/html" id="enabledTpl">
                    <input type="checkbox" lay-filter="enabled" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" {{d.enabled==='Y'?'checked':''}} />
                </script>
                <script type="text/html" id="tableBar">
                    <a sec:authorize="hasAuthority('dict:edit')" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                    <a sec:authorize="hasAuthority('dict:del')" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                </script>
            </div>
        </div>
        <div class="layui-col-md5">
            <div class="layui-card-body">
                <table id="dictValueTable" lay-filter="dictValueTable"></table>
            </div>
        </div>
    </div>
</div>
<script th:src="@{/static/layuiadmin/layui/layui.js}"></script>
<script th:inline="javascript">
    let ctx = [[${#request.getContextPath()}]];
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function() {
        let $ = layui.$
            ,form = layui.form
            ,table = layui.table;
        //监听搜索
        form.on('submit(dictKeySearch)', function(data){
            let field = data.field;
            //执行重载
            table.reload('dictTypeTable', {
                where: field
            });
            table.reload('dictValueTable',{
                data: []
            });
        });
        // 监听字典状态
        form.on('switch(enabled)', function (obj) {
            let id = obj.elem.value;
            let checked = obj.elem.checked ? 'Y' : 'N';
            active.changeDictEnabled(id,checked);
        });
        // 查询列表接口
        table.render({
            elem: '#dictTypeTable'
            ,height: 'full-110'
            ,url:  ctx + '/sysDict/list'
            ,page: true //开启分页,
            ,limit: 10 // 多少条
            ,request: {
                pageName: 'pageNum', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            ,parseData: function (result) {
                return {
                    "code": result.code,
                    "count": result.data.total,
                    "data": result.data.records
                }
            }
            ,response: {
                statusCode: 200 //成功的状态码，默认：0
            }
            ,cols: [[ //表头
                {field: 'id', title: 'ID', sort: true, fixed: 'left'}
                ,{field: 'dictName', title: '字典名称'}
                ,{field: 'dictKey', title: '字典别名'}
                ,{field: 'enabled', align: "center", sort: true, templet: '#enabledTpl', title: '状态'}
                ,{field: 'remark', title: '备注'}
                ,{toolbar: '#tableBar', title: '操作', width: 165, align:'center',fixed: 'right'}
            ]]
        });
        /**
         * 字典值列表
         */
        table.render({
            elem: '#dictValueTable'
            ,cellMinWidth: 80
            ,cols: [
                [
                    {field: 'dictValueName', title: '字典项名称'}
                    ,{field: 'dictValueKey', title: '字典项别名'}
                    ,{field: 'remark', title: '备注'}
                ]
            ]
            ,data: []
            ,even: true
        });
        //监听字典行单击事件
        table.on('row(dictTypeTable)', function(obj){
            table.reload('dictValueTable',{
                data: obj.data.dictValues
            });
            //标注选中样式
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
        });
        //监听行工具事件
        table.on('tool(dictTypeTable)', function(obj){
            let data = obj.data //获得当前行数据
                ,layEvent = obj.event;
            if(layEvent === 'edit'){
                active.editView(data.id);
            } else if(layEvent === 'del'){
                active.del(data.id);
            }
        });
        $('.layui-btn').on('click', function(){
            let type = $(this).data('type');
            active[type] && active[type].call(this);
        });
        /* 触发弹层 */
        let active = {
            /**
             * 添加
             * */
            addView: function(){
                layer.open({
                    type: 2,
                    title: "添加字典",
                    content: ctx + '/sysDict/addPage'
                    ,maxmin: true
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        let submit = layero.find('iframe').contents().find('#save-submit');
                        submit.trigger('click');
                    }
                });
            },
            /**
             * 编辑
             * @param id id
             * */
            editView: function(id){
                layer.open({
                    type: 2
                    ,title: '修改字典'
                    ,content: ctx + '/sysDict/editPage/' + id
                    ,maxmin: true
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        let submit = layero.find('iframe').contents().find('#save-submit');
                        submit.trigger('click');
                    }
                });
            },
            /**
             * 删除
             * @param id id
             */
            del: function(id){
                layer.confirm('真的删除行么', function(index){
                    //向服务端发送删除指令
                    $.ajax({
                        type: 'DELETE',
                        url:  ctx + '/sysDict/delKey/' + id,
                        contentType:'application/json;charset=UTF-8',
                        dataType: 'json',
                        success: function(result) {
                            layer.msg(result.message);
                            if (result.code === 200) {
                                layer.close(index);
                                table.reload('dictTypeTable');
                                table.reload('dictValueTable',{
                                    data: []
                                });
                            }
                        }
                    });
                });
            },
            /**
             * 修改字典状态
             * @param id 字典id
             * @param checked 状态
             */
            changeDictEnabled: function (id,checked) {
                $.ajax({
                    type: 'POST',
                    url:  ctx + '/sysDict/changeDict',
                    data: JSON.stringify({
                        id: id,
                        enabled: checked
                    }),
                    contentType:'application/json;charset=UTF-8',
                    dataType: 'json',
                    success: function(result) {
                        layer.msg(result.message);
                        if (result.code === 200) {
                            table.reload('dictTypeTable');
                        }
                    }
                });
            }
        }
    });
</script>
</body>
</html>
